<h2>Why is this an issue?</h2>
<p>Logging arguments should not require evaluation in order to avoid unnecessary performance overhead. When passing concatenated strings or string
interpolations directly into a logging method, the evaluation of these expressions occurs every time the logging method is called, regardless of the
log level. This can lead to inefficient code execution and increased resource consumption.</p>
<p>Instead, it is recommended to use the overload of the logger that accepts a log format and its arguments as separate parameters. By separating the
log format from the arguments, the evaluation of expressions can be deferred until it is necessary, based on the log level. This approach improves
performance by reducing unnecessary evaluations and ensures that logging statements are only evaluated when needed.</p>
<p>Furthermore, using a constant log format enhances observability and facilitates searchability in log aggregation and monitoring software.</p>
<p>The rule covers the following logging frameworks:</p>
<ul>
  <li> <a href="https://www.nuget.org/packages/Microsoft.Extensions.Logging">Microsoft.Extensions.Logging</a> </li>
  <li> <a href="https://www.nuget.org/packages/Castle.Core">Castle.Core</a> </li>
  <li> <a href="https://www.nuget.org/packages/log4net">log4net</a> </li>
  <li> <a href="https://www.nuget.org/packages/Serilog">Serilog</a> </li>
  <li> <a href="https://www.nuget.org/packages/NLog">Nlog</a> </li>
</ul>
<h2>How to fix it</h2>
<p>Use an overload that takes the log format and the parameters as separate arguments. The log format should be a constant string.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
logger.DebugFormat($"The value of the parameter is: {parameter}.");
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
logger.DebugFormat("The value of the parameter is: {Parameter}.", parameter);
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
  <li> Microsoft Learn - <a
  href="https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.interpolatedstringhandlerattribute">InterpolatedStringHandlerArgumentAttribute</a> </li>
</ul>
<h3>Benchmarks</h3>
<table>
  <colgroup>
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
  </colgroup>
  <thead>
    <tr>
      <th>Method</th>
      <th>Runtime</th>
      <th>Mean</th>
      <th>Standard Deviation</th>
      <th>Allocated</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td><p>CastleCoreLoggingTemplateNotConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>230.306 us</p></td>
      <td><p>2.7116 us</p></td>
      <td><p>479200 B</p></td>
    </tr>
    <tr>
      <td><p>CastleCoreLoggingTemplateConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>46.827 us</p></td>
      <td><p>1.4743 us</p></td>
      <td><p>560000 B</p></td>
    </tr>
    <tr>
      <td><p>CastleCoreLoggingTemplateNotConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>1,060.413 us</p></td>
      <td><p>32.3559 us</p></td>
      <td><p>1115276 B</p></td>
    </tr>
    <tr>
      <td><p>CastleCoreLoggingTemplateConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>93.697 us</p></td>
      <td><p>1.8201 us</p></td>
      <td><p>561650 B</p></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td><p>MSLoggingTemplateNotConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>333.246 us</p></td>
      <td><p>12.9214 us</p></td>
      <td><p>479200 B</p></td>
    </tr>
    <tr>
      <td><p>MSLoggingTemplateConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>441.118 us</p></td>
      <td><p>68.7999 us</p></td>
      <td><p>560000 B</p></td>
    </tr>
    <tr>
      <td><p>MSLoggingTemplateNotConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>1,542.076 us</p></td>
      <td><p>99.3423 us</p></td>
      <td><p>1115276 B</p></td>
    </tr>
    <tr>
      <td><p>MSLoggingTemplateConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>698.071 us</p></td>
      <td><p>18.6319 us</p></td>
      <td><p>561653 B</p></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td><p>NLogLoggingTemplateNotConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>178.789 us</p></td>
      <td><p>9.2528 us</p></td>
      <td><p>479200 B</p></td>
    </tr>
    <tr>
      <td><p>NLogLoggingTemplateConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>6.009 us</p></td>
      <td><p>1.3303 us</p></td>
      <td><p>-</p></td>
    </tr>
    <tr>
      <td><p>NLogLoggingTemplateNotConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>1,086.260 us</p></td>
      <td><p>44.1670 us</p></td>
      <td><p>1115276 B</p></td>
    </tr>
    <tr>
      <td><p>NLogLoggingTemplateConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>25.132 us</p></td>
      <td><p>0.5666 us</p></td>
      <td><p>-</p></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td><p>SerilogLoggingTemplateNotConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>234.460 us</p></td>
      <td><p>7.4831 us</p></td>
      <td><p>479200 B</p></td>
    </tr>
    <tr>
      <td><p>SerilogLoggingTemplateConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>49.854 us</p></td>
      <td><p>1.8232 us</p></td>
      <td><p>-</p></td>
    </tr>
    <tr>
      <td><p>SerilogLoggingTemplateNotConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>1,103.939 us</p></td>
      <td><p>47.0203 us</p></td>
      <td><p>1115276 B</p></td>
    </tr>
    <tr>
      <td><p>SerilogLoggingTemplateConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>35.752 us</p></td>
      <td><p>0.6022 us</p></td>
      <td><p>-</p></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td><p>Log4NetLoggingTemplateNotConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>255.754 us</p></td>
      <td><p>5.6046 us</p></td>
      <td><p>479200 B</p></td>
    </tr>
    <tr>
      <td><p>Log4NetLoggingTemplateConstant</p></td>
      <td><p>.NET 9.0</p></td>
      <td><p>46.425 us</p></td>
      <td><p>1.7087 us</p></td>
      <td><p>240000 B</p></td>
    </tr>
    <tr>
      <td><p>Log4NetLoggingTemplateNotConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>1,109.874 us</p></td>
      <td><p>23.4388 us</p></td>
      <td><p>1115276 B</p></td>
    </tr>
    <tr>
      <td><p>Log4NetLoggingTemplateConstant</p></td>
      <td><p>.NET Framework 4.8.1</p></td>
      <td><p>92.305 us</p></td>
      <td><p>2.4161 us</p></td>
      <td><p>240707 B</p></td>
    </tr>
  </tbody>
</table>
<h4>Glossary</h4>
<ul>
  <li> <a href="https://en.wikipedia.org/wiki/Arithmetic_mean">Mean</a> </li>
  <li> <a href="https://en.wikipedia.org/wiki/Standard_deviation">Standard Deviation</a> </li>
</ul>
<p>The results were generated by running the following snippet with <a href="https://github.com/dotnet/BenchmarkDotNet">BenchmarkDotNet</a>:</p>
<pre>
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;

[Params(10_000)]
public int Iterations;

private ILogger ms_logger;
private Castle.Core.Logging.ILogger cc_logger;
private log4net.ILog l4n_logger;
private Serilog.ILogger se_logger;
private NLog.ILogger nl_logger;

[GlobalSetup]
public void GlobalSetup()
{
    ms_logger = new LoggerFactory().CreateLogger&lt;LoggingTemplates&gt;();
    cc_logger = new Castle.Core.Logging.NullLogFactory().Create("Castle.Core.Logging");
    l4n_logger = log4net.LogManager.GetLogger(typeof(LoggingTemplates));
    se_logger = Serilog.Log.Logger;
    nl_logger = NLog.LogManager.GetLogger("NLog");
}

[BenchmarkCategory("Microsoft.Extensions.Logging")]
[Benchmark]
public void MSLoggingTemplateNotConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        ms_logger.LogInformation($"Param: {i}");
    }
}

[BenchmarkCategory("Microsoft.Extensions.Logging")]
[Benchmark]
public void MSLoggingTemplateConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        ms_logger.LogInformation("Param: {Parameter}", i);
    }
}

[BenchmarkCategory("Castle.Core.Logging")]
[Benchmark]
public void CastleCoreLoggingTemplateNotConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        cc_logger.Info($"Param: {i}");
    }
}

[BenchmarkCategory("Castle.Core.Logging")]
[Benchmark]
public void CastleCoreLoggingTemplateConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        cc_logger.InfoFormat("Param: {Parameter}", i);
    }
}

[BenchmarkCategory("log4net")]
[Benchmark]
public void Log4NetLoggingTemplateNotConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        l4n_logger.Info($"Param: {i}");
    }
}

[BenchmarkCategory("log4net")]
[Benchmark]
public void Log4NetLoggingTemplateConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        l4n_logger.InfoFormat("Param: {Parameter}", i);
    }
}

[BenchmarkCategory("Serilog")]
[Benchmark]
public void SerilogLoggingTemplateNotConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        se_logger.Information($"Param: {i}");
    }
}

[BenchmarkCategory("Serilog")]
[Benchmark]
public void SerilogLoggingTemplateConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        se_logger.Information("Param: {Parameter}", i);
    }
}

[BenchmarkCategory("NLog")]
[Benchmark]
public void NLogLoggingTemplateNotConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        nl_logger.Info($"Param: {i}");
    }
}

[BenchmarkCategory("NLog")]
[Benchmark]
public void NLogLoggingTemplateConstant()
{
    for (int i = 0; i &lt; Iterations; i++)
    {
        nl_logger.Info("Param: {Parameter}", i);
    }
}
</pre>
<p>Hardware Configuration:</p>
<pre>
BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.5247/22H2/2022Update)
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
  [Host]               : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256
  .NET 9.0             : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
  .NET Framework 4.8.1 : .NET Framework 4.8.1 (4.8.9282.0), X64 RyuJIT VectorSize=256
</pre>

